package casts.step
{
   import com.greensock.events.LoaderEvent;
   import com.greensock.loading.LoaderStatus;
   import com.greensock.loading.SWFLoader;
   
   import flash.display.MovieClip;
   
   import jp.nium.display.getInstanceById;
   import jp.progression.commands.display.AddChild;
   import jp.progression.commands.display.RemoveChild;

   /**
    * @author cjboy1984
    * @usage
    */
   public class AbstractStepContainer extends AbstractStep
   {
      // load
      protected var currenStep:MovieClip;
      protected var stepLoader:SWFLoader;
      
      public function AbstractStepContainer(initObject:Object = null)
      {
         super(initObject);
      }

      // --------------------- LINE ---------------------
      
      // step mover
      override public function changeToStep(stepId:String):void
      {
         // if loaded!
         var newStep:MovieClip = MovieClip(getInstanceById(stepId));
         // check if is the different step
         if (newStep == currenStep) return;
         if (newStep)
         {
            removeLoading();
            
            new RemoveChild(this, currenStep).execute();
            new AddChild(this, newStep).execute();
            
            currenStep = newStep;
         }
         else // if not loaded!
         {
            addLoading();
            
            tryStopLoader();
            var url:String = stepId + '.swf';
            stepLoader = new SWFLoader(url, { name:stepId, onProgress:onStepLoading, onComplete:onStepLoaded } );
            stepLoader.load();
         }
      }

      // ################### protected ##################
      
      protected function removeAllSteps():void
      {
      }
      
      // --------------------- LINE ---------------------

      override protected function addLoading():void
      {
         if (contains(mcLoading)) 
         {
            new RemoveChild(this, mcLoading).execute();
         }
      }
      
      override protected function removeLoading():void
      {
         if (!contains(mcLoading))
         {
            new AddChild(this, mcLoading).execute();
         }
      }
      
      // #################### private ###################
      
      private function tryStopLoader():void
      {
         if (stepLoader.status == LoaderStatus.LOADING)
         {
            stepLoader.cancel();
            stepLoader.dispose(true);
         }
      }
      
      private function onStepLoading(e:LoaderEvent):void
      {
         var perc:String = int(100 * stepLoader.bytesLoaded / stepLoader.bytesTotal).toString() + ' %';
         mcLoading.perc = perc;
      }
      
      private function onStepLoaded(e:LoaderEvent):void
      {
         removeLoading();
         
         var mc:MovieClip = MovieClip(getInstanceById(stepLoader.name));
         
         // change step
         new RemoveChild(this, currenStep).execute();
         new AddChild(this, mc).execute();
         
         currenStep = mc;
      }

      // --------------------- LINE ---------------------

   }

}